package com.hiwan.unions.adapter.fragmentAdapters;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hiwan.unions.R;
import com.hiwan.unions.adapter.bean.HomeBottonBean;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.Utils;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by llg on 2016/3/19.
 */
public class HomeAdapter extends BaseAdapter {
    private List<HomeBottonBean> cDates = new ArrayList<HomeBottonBean>();
    private Activity mContext;
    protected SharedPreferences userInfo;

    public HomeAdapter(Activity contex, List<HomeBottonBean> cDates) {
        this.mContext = contex;
        this.cDates = cDates;
    }

    @Override
    public int getCount() {
        return cDates.size();
    }

    @Override
    public Object getItem(int position) {
        return cDates.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_home_button, null);
            holder.ItemCityView = (TextView) convertView.findViewById(R.id.ItemCityView);
            holder.ItemDate = (TextView) convertView.findViewById(R.id.ItemDate);
            holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        setData(holder, position, convertView);
        return convertView;
    }

    /**
     * 设置viewHolder的数据  1型
     *
     * @param holder
     * @param itemIndex
     */
    private void setData(ViewHolder holder, final int itemIndex, View convertView) {
        holder.ItemCityView.setText(cDates.get(itemIndex).getTitle());
        holder.ItemDate.setText(cDates.get(itemIndex).getDate());
        Glide.with(mContext)
                .load(CheckUtils.isEmpty(cDates.get(itemIndex).getImage_net_url(), cDates.get(itemIndex).getImage_url()))
                .placeholder(R.mipmap.image_no_list)
                .error(R.mipmap.image_no_list)
                .centerCrop()
                .into(holder.imageView);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!CheckUtils.isEmpty(cDates.get(itemIndex).getJump_url())) {
                    Bundle bundle = new Bundle();
                    bundle.putString("method", "");
                    bundle.putString("url", cDates.get(itemIndex).getJump_url());
                    Utils.callHelpDeilsActivity(mContext, bundle);
                } else {
                    Request<JSONObject> request = NoHttp.createJsonObjectRequest(ConstsNet.detail, RequestMethod.POST);
                    request.add("uuid", cDates.get(itemIndex).getUuid());
                    CallServer.getRequestInstance().add(mContext, 0, request, objectListener, true);
                }
            }
        });
    }

    private HttpListener<JSONObject> objectListener = new HttpListener<JSONObject>() {
        @Override
        public void onSucceed(int what, Response<JSONObject> response) {
            JSONObject jsonObject = response.get();
            jsonObject = JsonUtil.getJSONObject(jsonObject, "data");
            Bundle bundle = new Bundle();
            bundle.putString("method", "");
            bundle.putBoolean("urlType", true);
            bundle.putString("url", Utils.callXw(jsonObject, "普惠资讯"));
            Utils.callHelpDeilsActivity(mContext, bundle);
        }

        @Override
        public void onFailed(int what, int errCode, String networkMillis, boolean type) {

        }
    };


    private class ViewHolder {
        private TextView ItemDate;
        private TextView ItemCityView;
        private ImageView imageView;

    }
}